package com.sec.etech.ism.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sec.etech.ism.model.IsmAppSetting;
import com.sec.etech.ism.model.IsmServiceDef;
import com.sec.etech.ism.service.IsmAppSettingService;
import com.sec.etech.ism.service.IsmCommonService;
import com.sec.etech.ism.service.IsmMethodService;
import com.sec.etech.ism.service.IsmServiceDefService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class IsmSendOrdersComponentCompleteSetsServiceImpl implements IsmMethodService {
    @Resource
    IsmCommonService ismCommonService;
    @Resource
    IsmAppSettingService ismAppSettingService;
    @Resource
    IsmServiceDefService ismServiceDefService;

    @Override
    public JSONObject call(String sysCode, String serviceCode, JSONObject busObj) throws Exception {
        JSONObject result = new JSONObject();
        String msg = this.sendOrdersComponentCompleteSets(sysCode, serviceCode, busObj);
        result.put("flag", msg);
        result.put("msg", msg);
        return result;
    }


    private String sendOrdersComponentCompleteSets(String sysCode, String serviceCode, JSONObject busObj) {
        Map<String, String> logMap = new HashMap<>(5);
        //服务信息
        IsmServiceDef serviceDef = null;
        try {
            serviceDef = ismServiceDefService.getByCode(serviceCode);
            //调用的应用信息
            IsmAppSetting appSetting = ismAppSettingService.getByCode(sysCode);
            String url = appSetting.getAppAddress() + "prodInfo/completeSetUnit";
            logMap.put("appName", this.methodName());
            logMap.put("url", url);
            JSONObject dataObject = ismCommonService.getOrdersComponentByOrdersComIds(
                    busObj.getLong("ordersid"), (List<String>) busObj.get("ordersComponentIds"));
            JSONObject pcsParams = new JSONObject();
            pcsParams.put("orders", dataObject.getString("orders"));
            List<JSONObject> ordersComList = dataObject.getObject("ordersComList", List.class);
            List<JSONObject> pcsOrdersComList = new ArrayList<>();
            for (JSONObject object : ordersComList) {
                JSONObject obj = new JSONObject();
                obj.put("catalogserial", object.get("catalogserial"));
                obj.put("serial", object.get("serial"));
                obj.put("componentNum", object.get("componentNum"));
                obj.put("componentName", object.get("componentName"));
                obj.put("completeSetName", object.get("completeSetName"));
                obj.put("testRunName", object.get("testRunName"));
                obj.put("componentSAPnum", object.get("componentSAPnum"));
                obj.put("companyWork", object.get("companyWork"));
                obj.put("tagNo", object.get("tagNo"));
                pcsOrdersComList.add(obj);
            }
            pcsParams.put("ordersComList", pcsOrdersComList);
            ismCommonService.checkResult(url, pcsParams.toJSONString(), serviceDef, null, busObj.toJSONString(), this.methodName(), logMap);
            return "success";
        } catch (Exception e) {
            log.error("机组下部套成套状态推送PCS接口异常。serviceDef.getCode()={}，serviceDef.getName()={}，logMap={}",
                    serviceDef.getCode(), serviceDef.getName(), logMap);
            e.printStackTrace();
            return "机组下部套成套状态推送PCS接口异常，请联系管理员处理！";
        }
    }

    @Override
    public String methodName() {
        return "sendOrdersComponentCompleteSets";
    }
}
